package com.farmer.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.farmer.mapper.ProjectapplyMapper;
import com.farmer.mapper.UserMapper;
import com.farmer.pojo.Projectapply;
import com.farmer.pojo.Projectapply;

import com.farmer.pojo.User;
import com.farmer.service.ProjectapplyService;
import com.farmer.utils.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.text.DateFormat;
import java.util.Date;
import java.util.List;

/**
* @author 86183
* @description 针对表【projectapply(扶贫项目申请表)】的数据库操作Service实现
* @createDate 2024-05-12 17:02:41
*/
@Service
public class ProjectapplyServiceImpl extends ServiceImpl<ProjectapplyMapper, Projectapply>
    implements ProjectapplyService{
    @Autowired
    private ProjectapplyMapper projectapplyMapper;
    @Autowired
    private JwtHelper jwtHelper;
    @Autowired
    private UserMapper userMapper;


    @Override
    public Result add(String token, Projectapply projectapply) {
        int userid = jwtHelper.getUserId(token).intValue();
        User user = userMapper.selectById(userid);
        projectapply.setUserId(userid);
        projectapply.setUserName(user.getName());
        projectapply.setTime(DateFormat.getDateInstance().format(new Date()));
        projectapply.setStatus("审核不通过");
        projectapplyMapper.insert(projectapply);
        return Result.ok(null);

    }

    @Override
    public Result delete(Integer id) {
        projectapplyMapper.deleteById(id);
        return null;
    }


    @Override
    public Result update(Projectapply projectapply) {
        projectapplyMapper.updateById(projectapply);
//        如果审核通过，需要把用户的身份改成贫困户
        if("审核通过".equals(projectapply.getStatus())){
            User user = userMapper.selectById(projectapply.getUserId());
            if (user!=null){
                user.setLevel(LevelEnum.POOR.level);
                userMapper.updateById(user);
            }
        }
        return Result.ok(null);
    }


    @Override
    public Result getById(Integer id) {
        Projectapply projectapply = projectapplyMapper.selectById(id);
        if (projectapply != null ) {
            return Result.build(projectapply, 111, "查询成功");
        } else {
            return Result.build(null, 101, "查询失败");
        }
    }

    @Override
    public Result getAll() {
        List<Projectapply> projectapplies = projectapplyMapper.selectList(null);
        if (projectapplies != null && !projectapplies.isEmpty()) {
            return Result.build(projectapplies, 111, "查询成功");
        } else {
            return Result.build(null, 101, "查询失败");
        }
    }

    @Override
    public Result getByTitleLike(String status){
        LambdaQueryWrapper<Projectapply> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.like(Projectapply::getStatus,status);
        List<Projectapply> projectapplys = projectapplyMapper.selectList(lambdaQueryWrapper);
        if (projectapplys!=null&&!projectapplys.isEmpty()){
            return Result.build(projectapplys,111,"查询成功");
        }else{
            return Result.build(null,101,"未找到匹配信息");
        }
    }



    @Override
    public Result uploadFile(Integer id, MultipartFile file)  {
        try {
            String url = Upload.uploadImage(file);
            Projectapply projectapply = projectapplyMapper.selectById(id);
            projectapply.setId(id);
            projectapply.setFile(url);
            updateById(projectapply);
            return Result.build(url, ResultCodeEnum.SUCCESS);
        } catch (IOException e) {
            e.printStackTrace();
            return Result.build(null, ResultCodeEnum.FAULT);
        }
    }

    /*
     * 分页查询*/
    @Override
    public Result page(Integer pageNum, Integer pageSize){
        // 创建分页构造对象
        Page<Projectapply> page = new Page<>(pageNum, pageSize);
        // 执行分页查询
        IPage<Projectapply> result = projectapplyMapper.selectPage(page, null);
        // 封装到统一响应体Result中
        return Result.ok(result);
    }
    
    @Override
    public Result getProjectapplyListByToken(String token) {
        // 验证token的有效性
        int userId = jwtHelper.getUserId(token).intValue();
        LambdaQueryWrapper<Projectapply> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Projectapply::getUserId, userId);
        // 根据用户ID获取订单列表
        List<Projectapply> projectapplyList = projectapplyMapper.selectList(lambdaQueryWrapper);
        if (projectapplyList != null && !projectapplyList.isEmpty()) {
            return Result.build(projectapplyList, 111, "查询成功");
        } else {
            return Result.build(null, 101, "查询失败,该用户没有扶贫项目申请");
        }
    }
}




